﻿using Domain.Event;
using Domain.IRepositorys;
using Domain.Model;
using Domain.Services;
using Domain.Types;
using Infrastructure.DomainEvent;
using Infrastructure.Error;
using Infrastructure.UniOfWorkBase;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using OtherLib.AliPay;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Utility;

namespace Application.Event
{
    /// <summary>
    /// 提现完成
    /// </summary>
    public class FinishWithDrawEventHandler : IDomainEventHandler<FinishWithDrawEvent>
    {
        public IWalletRepository walletRepository { get; set; }
        public ITXJLLogRepository tXJLLogRepository { get; set; }
        public IUserRepository userRepository { get; set; }
        public IWithDrawRepository withDrawRepository { get; set; }
        public MsgPulishService msgPulishService { get; set; }
        public IEFUnitOfWork efUnitOfWork { get; set; }
        public WalletService walletService { get; set; }
        public ILogger<WithDrawEventHandler> _logger { get; set; }
        public override Task HandleAysnc(FinishWithDrawEvent domainEvent, CancellationToken cancellationToken = default)
        {
            var u = userRepository.GetByKey(domainEvent.UserID);
            var Num = tXJLLogRepository.Entities.Count(i => i.UserID == domainEvent.UserID);
            if (Num > 5) return Task.CompletedTask;

            //上线拿十二元奖励处理
            TXJLLogAggregate tXJLLogAggregate = new TXJLLogAggregate();
            tXJLLogAggregate.UserID = domainEvent.UserID;
            tXJLLogAggregate.WithDrawIdentity = domainEvent.WithDrawIdentity;
            tXJLLogAggregate.SuperorUserID = u.SuperorID;

            tXJLLogRepository.Insert(tXJLLogAggregate);
            var fee = 0;

            switch (Num)
            {
                case 0:
                    fee = u.GetLevelConfig<int>("十二元现金奖励.好友首次提现");
                    break;
                case 1:
                    fee = u.GetLevelConfig<int>("十二元现金奖励.好友第二提现");
                    break;
                case 2:
                    fee = u.GetLevelConfig<int>("十二元现金奖励.好友第三提现");
                    break;
                case 3:
                    fee = u.GetLevelConfig<int>("十二元现金奖励.好友第四提现");
                    break;
                case 4:
                    fee = u.GetLevelConfig<int>("十二元现金奖励.好友第五提现");
                    break;
                //case 5:
                //    fee = u.GetLevelConfig<int>("十二元现金奖励.好友第六提现");
                //    break;
                default:
                    return Task.CompletedTask;
                    
            }


            walletService.ChangeInComeWallet(tXJLLogAggregate.SuperorUserID, fee,0, -fee, domainEvent.WithDrawIdentity.ToString(), tXJLLogAggregate.UserID, FinancialBizType.十二元现金奖励
                , "十二元现金奖励", $"{domainEvent.UserID}第{Num}次提现");

            efUnitOfWork.Commit();

            return Task.CompletedTask;
        }
    }
}
